﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ReliableSession;
using System.Threading;
using System.IO;
using ReliableSession.src;
using log4net;

namespace Tests.NetTest
{
    public partial class NetSpeedTest : UserControl
    {
        private Transport server;
        private Transport client;
        private int serverPort = 54545;
        private static readonly ILog log = LogManager.GetLogger(typeof(NetSpeedTest));
        private Thread serverThread;
        private Thread clientThread;
        private Transport acceptedSocket;

        public NetSpeedTest()
        {
            InitializeComponent();
            timer1.Enabled = true;
            timer1.Interval = 1000;
        }

        public void acceptThread()
        {
            try
            {
                Thread.CurrentThread.Name = "Server";
                acceptedSocket = server.accept();
                acceptedSocket.m_debug = true;
                byte[] data = new byte[64 * 1024];
                //for(int i=1000; i< 2500; i++)
                using (BinaryReader reader = new BinaryReader(File.Open("../../testData/test1.data", FileMode.Open)))
                {
                    int len = 0;
                    while ((len = reader.Read(data, 0, 450)) > 0)
                    {
                        acceptedSocket.send(data, 0, len);
                    }
                }
                Thread.Sleep(500);
                acceptedSocket.close();
                TransportStatistics stats = acceptedSocket.m_statistics;
                log.Info(stats.ToString());
            }
            catch (Exception e)
            {
                log.Error(e);
            }
        }

        private void runClient( )
        {
            client = new Transport();
            client.openClientSocket("127.0.0.1", serverPort);
            //client.openClientSocket("192.168.0.192", serverPort);
            byte[] d = new byte[64 * 1024];
            int chunk = 0;
            int totalLen = 0;
            using (BinaryWriter file = new BinaryWriter(File.Open("out.dat", FileMode.Create)))
            {
                while (true)
                {
                    int len = client.read(d, 0, d.Length);
                    if (len > 0)
                    {
                        //String str = Encoding.ASCII.GetString(d, 0, len);
                        file.Write(d, 0, len);
                        file.Flush();
                        chunk += len;
                        if (chunk > 64 * 1024)
                        {
                            totalLen += chunk;
                            chunk = 0;
                            log.Info("Got data = " + totalLen);
                        }
                        //log.Info(str);
                    }
                }
            }
        }

        private void m_startNetTest_Click(object sender, EventArgs e)
        {
            if (server == null)
            {
                server = new Transport();
                server.openServerSocket(serverPort);
                serverThread = new Thread(new ThreadStart(acceptThread));
                serverThread.Start();
                Thread.Sleep(500);
                clientThread = new Thread(new ThreadStart(runClient));
                clientThread.Start();
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (acceptedSocket != null && client != null)
            {
                m_transportStatsLabel.Text = "Server=" + acceptedSocket.m_statistics.ToString() + "\nClient=" + client.m_statistics.ToString();
            }
        }
    }
}
